#!/bin/bash

version=0.8
progname=gotexi

outfile=go.texi
title="Go programming language"
golink="golang.org"

usage () {
    echo usage: gotexi [options]
    echo
    echo Options:
    echo -o, --output "<filename>:" output file
    echo -h, --help: displays this help message
    exit $1
}

# process command-line options
for i; do
    case $i in
	-h|--help) usage 0 ;;
	-o|--output) shift
	    [ $# -eq 0 ] && usage 1
	    exec 7>&1
	    exec 1>$1
	    outfile=$1
	    shift ;;  # from now on output to the output file
    esac
done

# check that the GOROOT variable is set and points to a directory
if [ "$GOROOT" == "" ]; then
    echo 1>&2 GOROOT env variable not set!; 
    exit 1
elif [ ! -d $GOROOT/src/pkg ]; then 
    echo 1>&2 "Go source directory does not exist!"
    echo 1>&2 Please check that your GOROOT variable is set correctly
    exit 2
fi

# check that godoc exists
if ! which godoc; then
    echo 1>&2 Found no godoc in your system
    echo 1>&2 Aborting...
    exit 7
fi

# generate the list of packages in this dir. tree
pkgs=`(cd $GOROOT/src/pkg && find . -name "*.go") | grep -v '.*_test\.go' | \
    sed 's/^\.\///
s/\/[^\/]\+$//' | sort | uniq`
if [ "pkgs" == "" ]; then
    echo 1>&2 No Go source file under this directory!
    echo 1>&2 Are you sure your GOROOT variable is set properly?
    exit 5
fi

# get Go changeset information using hg, and if it is not installed in the
# system, do not enter the corresponding information on the output
hginfo=`(cd $GOROOT && hg log 2>/dev/null | hg 2>/dev/null heads | grep '^changeset\|^date')`
if [ "$hginfo" == "" ]; then
    echo 1>&2 "Could not get repository information; do you have Mercurial installed?"
    echo 1>&2 "Proceeding anyway..."
fi

# output TexInfo preamble
echo \\input texinfo   @c -\*-texinfo-\*-
echo @comment  $outfile, generated by $progname on `date`
if [ "$hginfo" != "" ]; then
    echo @comment  go commit:  $hginfo
fi
echo @comment %**start of header
echo @setfilename $outfile
echo @settitle $title
echo @comment %**end of header
#echo @copying
#echo @end copying
echo 
echo @dircategory $title
echo @direntry
echo "* go: (go) $title."
echo @end direntry
echo 
echo @titlepage
echo @title $title
if [ "$hginfo" != "" ]; then
    echo @subtitle for commit $hginfo
fi
#echo @author 
echo @page
echo @vskip 0pt plus 1filll
#echo @insertcopying
echo @end titlepage
echo @defindex pk
echo @defindex ct
echo 
echo @contents
echo 
echo @ifnottex
echo @node Top
echo @top $title
echo
echo This is the TexInfo version of the documentation for the Go programming 
echo language. It was automatically generated by the program @code{goinfo} from
echo the original Go online documentation, available from $golink.
echo
if [ "$hginfo" != "" ]; then
    echo These documents refer to Go version: 
    echo
    echo $hginfo
fi
echo
echo    Packages:
echo
echo @end ifnottex
echo

# print main menu
echo
echo @menu
for p in $pkgs; do
    echo \* $p::
done
echo \* Indices::
echo @end menu
echo

# output documentation for the packages
for p in $pkgs; do
    ./texigopkg $p
done

# add nodes for the indices
echo
echo @node Indices
echo @chapter Indices
echo
echo @menu
echo \* Package Index::
echo \* Type Index::
echo \* Function Index::
echo \* Constants Index::
echo \* Variables Index::
echo @end menu
echo

echo
echo @node Package Index, Type Index, Variables Index, Indices
echo @unnumbered Package Index
echo
echo @printindex pk
echo
echo
echo @node Type Index, Function Index, Package Index, Indices
echo @unnumbered Type Index
echo
echo @printindex tp
echo
echo
echo @node Function Index, Constants Index, Type Index, Indices
echo @unnumbered Function Index
echo
echo @printindex fn
echo
echo
echo @node Constants Index, Variables Index, Function Index, Indices
echo @unnumbered Constants Index
echo
echo @printindex ct
echo
echo
echo @node Variables Index, Package Index, Constants Index, Indices
echo @unnumbered Variables Index
echo
echo @printindex vr
echo

# ... and finish
echo @bye
